// TypeScript file
module CdCircle {

    export class Circle {

        constructor() {
            //this.init(showPanel,x,y,time);
        }
        /**cd图形 */
        private shape: egret.Shape;
        /**矩形遮罩 */
        private rectMask: egret.Shape;
        /**旋转角度 */
        private angle: number;
        /**cd时间,单位:秒 */
        public cdTime: number;
        /**Cd时间，单位：毫秒 */
        public cdTimeMS: number;
        private cdText: egret.TextField;
        /**是否处于cd */
        public inCdTime: boolean = false;
        /**上一帧*/
        public lastTimer;
        /**
         * 初始化cd图形
         * @param showPanel 显示面板
         * @param x x坐标
         * @param y y坐标
         * @param time cd时间
         * 
         */
        public init(showPanel, x, y, time) {
            //初始角度
            this.angle = 270;
            this.cdTime = time;
            this.inCdTime = true;
            this.cdTimeMS = time * 1000;
            //图形与文本设置
            this.shape = new egret.Shape();

            this.cdText = new egret.TextField();
            this.cdText.x = x - 20;
            this.cdText.y = y - 15;
            this.cdText.size = 30;

            this.lastTimer = egret.getTimer();
            this.shape.addEventListener(egret.Event.ENTER_FRAME, () => { this.changeGraphic(x, y) }, this);
            //加载图形
            showPanel.addChild(this.shape);
            showPanel.addChild(this.cdText);
            //设置遮罩
            this.rectMask = new egret.Shape();
            this.rectMask.graphics.beginFill(0x000000, 0.8);
            this.rectMask.graphics.drawRect(x-75, y-75, 150, 150);  
            this.rectMask.graphics.endFill();
            showPanel.addChild(this.rectMask);
            this.shape.mask = this.rectMask;
        }
        public changeGraphic(x, y) {
            let now = egret.getTimer();
            let pass = now - this.lastTimer;
            this.lastTimer = now;
            this.cdTime -= pass / 1000;
            this.cdText.text = this.cdTime.toFixed(2);
            //圆形图案cd减少
            this.angle += 360 * pass / this.cdTimeMS;
            this.shape.graphics.clear();
            this.shape.graphics.beginFill(0x000000, 0.3);
            this.shape.graphics.moveTo(x, y);
            this.shape.graphics.lineTo(x, y);
            this.shape.graphics.drawArc(x, y, 200, this.angle * Math.PI / 180, -Math.PI / 2);
            this.shape.graphics.lineTo(x, y);
            this.shape.graphics.endFill();
            this.angle %= 360;
            if (this.cdTime <= 0) {
                if (this.shape.parent) {
                    this.shape.removeEventListener(egret.Event.ENTER_FRAME, () => { this.changeGraphic(x, y) }, this);
                    this.shape.parent.removeChild(this.shape);
                }
                if (this.cdText.parent)
                    this.cdText.parent.removeChild(this.cdText);
                this.inCdTime = false;
            }
        }


    }
}